package com.sangmo.fts.common;

import com.sangmo.boot.framework.data.idm.IdentityManager;
import com.sangmo.boot.framework.data.runtime.SystemSetting;
import com.sangmo.boot.framework.http.SyncHttpClient;
import com.sangmo.boot.framework.plugin.dataview.def.types.CollectionType;
import com.sangmo.boot.framework.plugin.dataview.def.types.DateType;
import com.sangmo.boot.framework.plugin.dataview.def.types.DecimalType;
import com.sangmo.boot.framework.plugin.dataview.def.types.IntegerType;
import com.sangmo.boot.framework.plugin.dataview.def.types.LongType;
import com.sangmo.boot.framework.plugin.dataview.def.types.StringType;
import com.sangmo.boot.framework.plugin.dataview.def.types.TypeRegister;
import com.sangmo.fts.common.authority.AuthorityFilterChain;
import com.sangmo.fts.common.authority.filters.AuthorizeRefFilter;
import com.sangmo.fts.common.authority.filters.PropertyCheckFilter;
import com.sangmo.fts.common.authority.filters.UseRefFilter;
import com.sangmo.fts.common.authority.filters.UserFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class CommonConfig {

    @PostConstruct
    private void init() {
        this.registerDataTypes();
        this.registerAuthority();
    }

    private void registerAuthority() {
        AuthorityFilterChain.getInstance().register(userFilter());
        AuthorityFilterChain.getInstance().register(authorizeRefFilter());
        AuthorityFilterChain.getInstance().register(useRefFilter());
        AuthorityFilterChain.getInstance().register(propertyCheckFilter());
    }

    private void registerDataTypes() {
        TypeRegister register = TypeRegister.getInstance();

        register.add(new StringType(IConstants.DATA_TYPE.STRING_URL, 0, 100));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_MOBILE, "^[0-9]{11}$"));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_EMAIL, "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,})$"));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_PASSWORD, "^(?=.*([0-9a-zA-Z].*))(?=.*[0-9].*)[a-zA-Z0-9-*/+.~!@#$%^&*()]{6,20}$"));

        register.add(new StringType(IConstants.DATA_TYPE.STRING_ID, 0, 32));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_CODE, 0, 20));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_NAME, 0, 100));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_ADDRESS, 0, 500));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_DESCRIPTION, 0, 500));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_10, 0, 10));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_50, 0, 50));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_100, 0, 100));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_2000, 0, 2000));
        register.add(new StringType(IConstants.DATA_TYPE.STRING_STATE, IConstants.ENABLE_STATE.class));

        register.add(new DateType(IConstants.DATA_TYPE.DATE_BEGIN,DateType.Types.BEGIN));
        register.add(new DateType(IConstants.DATA_TYPE.DATE_END, DateType.Types.END));
        register.add(new DateType(IConstants.DATA_TYPE.DATE));
        register.add(new DateType(IConstants.DATA_TYPE.DATE_FUTURE, DateType.Types.FUTURE));
        register.add(new DateType(IConstants.DATA_TYPE.DATE_PAST, DateType.Types.PAST));

        register.add(new DecimalType(IConstants.DATA_TYPE.DECIMAL_AREA, 0, 999999999));
        register.add(new DecimalType(IConstants.DATA_TYPE.DECIMAL_POSITIVE_MONEY, 0, 999999999));
        register.add(new DecimalType(IConstants.DATA_TYPE.DECIMAL_MONEY));

        register.add(new IntegerType(IConstants.DATA_TYPE.INTEGER_POSITIVE, 0, 999999999));
        register.add(new IntegerType(IConstants.DATA_TYPE.INTEGER));
        register.add(new LongType(IConstants.DATA_TYPE.LONG_POSITIVE, 0, 9223372036854775807L));
        register.add(new LongType(IConstants.DATA_TYPE.LONG));
        register.add(new DecimalType(IConstants.DATA_TYPE.DECIMAL_1, 0, 1));

        register.add(new CollectionType(IConstants.DATA_TYPE.COLLECTION_4, 4,4));
        register.add(new CollectionType(IConstants.DATA_TYPE.COLLECTION_1_4, 1,4));
        register.add(new CollectionType(IConstants.DATA_TYPE.COLLECTION, 1,999999999));

    }

    @Bean
    public UserFilter userFilter() {
        return new UserFilter();
    }

    @Bean
    public UseRefFilter useRefFilter() {
        return new UseRefFilter();
    }

    @Bean
    public PropertyCheckFilter propertyCheckFilter() {
        return new PropertyCheckFilter();
    }

    @Bean
    public AuthorizeRefFilter authorizeRefFilter() {
        return new AuthorizeRefFilter();
    }


    @Bean
    public IdentityManager identityManager() {
        IdentityManager identityManager = new IdentityManager();
        identityManager.setTableName("id_repository");

        return identityManager;
    }

    @Bean
    public SystemSetting systemSetting() {
        SystemSetting systemSetting = new SystemSetting();
        systemSetting.setTableName("runtime_setting");

        return systemSetting;
    }

    @Bean
    public SyncHttpClient httpClient() {
        return new SyncHttpClient();
    }
}
